//
//  BQMissionCenterViewController.m
//  Adorable_Things
//
//  Created by ybq on 16/1/14.
//  Copyright © 2016年 Banana Inc. All rights reserved.
//

#import "BQMissionCenterViewController.h"
#import "BQMissionCenterTableViewCell.h"
#import "BQMissionCenterModel.h"
#import "BQNavViewCommon.h"
#import "BQMissionHeaderView.h"
#import "BQCheckin_RuleViewController.h"


@interface BQMissionCenterViewController () <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) BQNavViewCommon *navView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BQMissionCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 因侧边滑返回时灵时不灵,加这句以防万一
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    _dataArray = [NSMutableArray array];
    [self requestData];
    
    _navView = [[BQNavViewCommon alloc] initWithFrame:CGRectMake(0, 20, kWidth, 44)];
    [_navView.backButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _navView.titleLabel.text = @"任务中心";
    _navView.checkin_RuleButton.hidden = NO;
    [_navView.checkin_RuleButton setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];
    [_navView.checkin_RuleButton setTitle:@"签到规则" forState:(UIControlStateNormal)];
    [_navView.checkin_RuleButton addTarget:self action:@selector(checkin_RuleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:_navView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64) style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight = 120;
    _tableView.showsVerticalScrollIndicator = NO;
    BQMissionHeaderView *headerView = [[BQMissionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kWidth / 1.785)];
    self.tableView.tableHeaderView = headerView;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"BQMissionCenterTableViewCell" bundle:nil] forCellReuseIdentifier:@"BQMissionCenterTableViewCell"];
    
}

#pragma mark -- navView按钮点击事件
- (void)backAction:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)checkin_RuleButtonAction:(UIButton *)button {
    BQCheckin_RuleViewController *checkinVC = [[BQCheckin_RuleViewController alloc] init];
    [self.navigationController pushViewController:checkinVC animated:YES];
}

- (void)requestData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableDictionary *parameterDic = [NSMutableDictionary dictionary];
    parameterDic[@"page"] = @"0";
    parameterDic[@"page_size"] = @"20";
    [manager POST:@"http://api.menghuoapp.com/v1/mission/list" parameters:parameterDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *dataArray = responseObject[@"data"];
        for (NSDictionary *dic in dataArray) {
            BQMissionCenterModel *model = [[BQMissionCenterModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_dataArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BQMissionCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BQMissionCenterTableViewCell" forIndexPath:indexPath];
    BQMissionCenterModel *model = _dataArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
